class DBTools {
	constructor(options) {
		options = options || {};
		this.database = null;
		this.DateBaseName = options.DateBaseName || 'first';
		this.Version = options.Version || '1.0';
		this.Description = options.Description || 'LeaderInfoSystem';
		this.DataBaseSize = options.DataBaseSize || 10 * 1024 * 1024;
		this.path = options.path || '_doc/test.db'
		this.init();
	}
	/**
	 * 初始化数据库
	 */
	init() {
		plus.sqlite.openDatabase({
			name: 'first',
			path: '_doc/test.db',
			success: function() {
				//outLine('openDatabase success!');
			},
			fail: function(e) {
				//outLine('openDatabase failed: ' + JSON.stringify(e));
			}
		});
	}

	isOpenDB() {
		if (plus.sqlite.isOpenDatabase({
				name: 'first',
				path: '_doc/test.db',
			})) {
			plus.nativeUI.alert('Opened!');
		} else {
			plus.nativeUI.alert('Unopened!');
		}
	}

	// 关闭数据库
	closeDB(){
	plus.sqlite.closeDatabase({
		name: 'first',
		success: function(){
			console.log('关闭成功')
		},
		fail: function(e){
			console.log('关闭失败')
		}
	});
}

	/**
	 * 读取表数据
	 * @param {*} tableName 表名
	 * @param {*} condition 查询条件   'where name="jiekzou"'
	 */
	read(tableName, condition) {		
		var _me = this;
		var _re = [];
		var _condition = _me.isString(condition) ? condition : '';
		let sqlString = 'SELECT * FROM ' + tableName + ' ' + _condition + ' ';
		return new Promise(function(resovle,reject){
			plus.sqlite.selectSql({
				name: 'first',
				sql: sqlString,
				success: function(results){
					if (results.length>0) {
					  _re = _me.toArray(results);
					  //console.log(_re)
					  resovle(_re);
					} else {
					  resovle([]);
					}
				},
				fail: function(e){
					reject(['executeSql failed: '+JSON.stringify(e),'']);
				}
			});
		})
		
	}
	/**
	 * 读取表数据
	 * @param {*} tableName 表名
	 * @param {*} field 查询字段，逗号隔开
	 * @param {*} condition 查询条件   'where name="jiekzou"'
	 * resovle 要返回的值是一个数组,格式是[err,data]
	 */
	readField(tableName, field, condition) {
		var _me = this;
		var _condition = _me.isString(condition) ? condition : '';
		var _re = [];
		let sqlString = 'SELECT ' + field + ' FROM ' + tableName + ' ' + _condition + ' ';

		return new Promise(function(resovle,reject){
			plus.sqlite.selectSql({
				name: 'first',
				sql: sqlString,
				success: function(results){
					if (results.length>0) {
					  _re = _me.toArray(results);

					  resovle(_re);
					} else {
					  resovle([]);
					}
				},
				fail: function(e){
					reject(['executeSql failed: '+JSON.stringify(e),'']);
				}
			});
		})
	}

	
	/**
	 * 读取表数据
	 * @param {*} tableName 表名
	 * @param {*} field 查询字段，逗号隔开
	 * @param {*} condition 查询条件   'where name="jiekzou"'
	 * resovle 要返回的值是一个数组,格式是[err,data]
	 */
	readFieldByCommand(sqlStringCommand) {
		var _me = this;
		var _re = [];
		let sqlString = sqlStringCommand;

		return new Promise(function(resovle,reject){
			plus.sqlite.selectSql({
				name: 'first',
				sql: sqlString,
				success: function(results){
					if (results.length>0) {
					  _re = _me.toArray(results);

					  resovle(_re);
					} else {
					  resovle([]);
					}
				},
				fail: function(e){
					reject(['executeSql failed: '+JSON.stringify(e),'']);
				}
			});
		})
	}


	// 更新字符处理
	splitU(obj) {
		var _arr = [];
		for (var t in obj) {
			_arr.push(t + '=\'' + obj[t] + '\'');
		}
		return _arr.join(',');
	}
	// 添加字符处理
	split(obj) {
		var _arr = [];
		for (var m in obj) {
			_arr.push('\'' + obj[m] + '\'');
		}
		return _arr.join(',');
	}
	isFunction(callback) {
		return !!(
			typeof callback != 'undefined' && callback.constructor == Function
		);
	}
	isString(string) {
		return typeof string == 'string';
	}
	toArray(obj) {
		var _arr = [];
		var _len = obj.length;
		if (_len > 0) {
			for (var i = 0; i < _len; i++) {

				_arr.push([null, obj[i]]);
			}
		}
		return _arr;
	}
}

export default DBTools;
